package org.lanyu.springainovel.novel.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.lanyu.springainovel.common.dto.StreamGenerationRequest;
import org.lanyu.springainovel.common.vo.RestVO;
import org.lanyu.springainovel.novel.entity.BookRole;
import org.lanyu.springainovel.novel.service.BookRoleService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.List;
import java.util.Map;

/**
 * 人设管理控制器
 */
@RestController
@RequestMapping("/book-roles")
@RequiredArgsConstructor
@Tag(name = "人设管理", description = "人设相关的API接口")
public class BookRoleController {

    private final BookRoleService bookRoleService;

    @GetMapping("/versions")
    @Operation(summary = "获取人设历史版本", description = "获取指定书籍人设的所有历史版本")
    public RestVO<List<BookRole>> getCharacterProfileVersions(
            @RequestParam Long bookId) {
        List<BookRole> result = bookRoleService.getCharacterProfileVersions(bookId);
        return RestVO.success(result);
    }

    @GetMapping("/version")
    @Operation(summary = "获取指定版本的人设", description = "根据书籍ID和版本获取人设")
    public RestVO<BookRole> getCharacterProfilesByBookIdAndVersion(
            @RequestParam Long bookId, @RequestParam Long version) {
        BookRole result = bookRoleService.getCharacterProfilesByBookIdAndVersion(bookId,version);
        return RestVO.success(result);
    }

    @GetMapping("/latest")
    @Operation(summary = "获取最新人设", description = "根据书籍ID获取最新版本的人设")
    public RestVO<BookRole> getLatestCharacterProfile(
            @RequestParam Long bookId) {
        BookRole result = bookRoleService.getLatestCharacterProfile(bookId);
        return RestVO.success(result);
    }

    @GetMapping("/active")
    @Operation(summary = "获取当前启用状态的角色", description = "根据书籍ID获取当前启用状态的角色")
    public RestVO<BookRole> getActiveCharacterProfile(
            @RequestParam Long bookId) {
        BookRole result = bookRoleService.getActiveCharacterProfile(bookId);
        return RestVO.success(result);
    }

    @PostMapping
    @Operation(summary = "添加人设版本", description = "为指定书籍添加人设版本")
    public RestVO<BookRole> addCharacterProfile(
            @RequestBody BookRole bookRole) {
        BookRole result = bookRoleService.addCharacterProfile(bookRole);
        return RestVO.success(result);
    }

    @DeleteMapping
    @Operation(summary = "删除指定版本的人设", description = "根据书籍ID和版本号删除人设")
    public RestVO<Boolean> deleteCharacterProfileByBookIdAndVersion(
            @RequestParam Long bookId, @RequestParam Long version) {
        boolean result = bookRoleService.deleteCharacterProfileByBookIdAndVersion(bookId, version);
        return RestVO.success(result);
    }

    @GetMapping("/faction-stats")
    @Operation(summary = "获取派系统计", description = "获取书籍中所有派系及其人设数量")
    public RestVO<Map<String, Integer>> getFactionStatistics(
            @RequestParam Long bookId) {
        Map<String, Integer> result = bookRoleService.getFactionStatistics(bookId);
        return RestVO.success(result);
    }

    @PostMapping(value = "/batch-generate-stream", produces = MediaType.TEXT_PLAIN_VALUE + ";charset=UTF-8")
    @Operation(summary = "批量生成角色的流式接口", description = "批量生成角色的流式接口")
    public Flux<String> batchGenerateCharacterProfilesStream(
            @RequestBody StreamGenerationRequest request) {
        return bookRoleService.batchGenerateCharacterProfilesStream(request);
    }
    
    @PutMapping("/version")
    @Operation(summary = "更新指定版本的人设", description = "通过指定书籍ID和版本号更新人设信息")
    public RestVO<BookRole> updateCharacterProfileByVersion(
            @RequestBody BookRole bookRole) {
        BookRole result = bookRoleService.updateCharacterProfileByVersion(bookRole);
        return RestVO.success(result);
    }
    
    @PostMapping("/archive")
    @Operation(summary = "归档人设", description = "归档指定版本的版人设，创建新记录")
    public RestVO<BookRole> archiveCharacterProfile(
            @RequestBody BookRole bookRole) {
        BookRole result = bookRoleService.archiveCharacterProfile(bookRole);
        return RestVO.success(result);
    }
}